<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	if ( !function_exists('read_options'))
	{
			function read_options($opt='', $ret_type = 'ARRAY') {
					$_ci = & get_instance();
					$_ci->load->database(); 
					
					$_arr = array();
					$_obj = new stdClass();
					
					$opt_list = preg_match('#,#', $opt) ? preg_split('#\s*,\s*#', $opt) : $opt;
					
					$sql_opts = '';
					if(is_array($opt_list))
					{
							foreach($opt_list as $o) $sql_opts .= ($sql_opts?', ':'') . "'".$o."'";
					}
					elseif($opt_list!='')
					{
							$sql_opts .= "'".$opt_list."'"; 
					}
					
					$dmc = $_ci->config->item('datamapper');
					if($sql_opts!='')
					{
							$sql = "SELECT *
							FROM " . @$dmc['prefix'] . "options
							WHERE name IN ($sql_opts);";	
					}
					else
					{
							$sql = "SELECT *
							FROM " . @$dmc['prefix'] . "options
							WHERE 1;";
					}
					$query = $_ci->db->query($sql);
					foreach ($query->result() as $row)
					{
							if($ret_type == 'OBJECT')
							{
									$_obj->{$row->name} = $row->value;
							}
							else
							{
									$_arr[$row->name] = $row->value;
							}
					}
					
					if(is_array($opt_list))
					{
							foreach($opt_list as $o) 
							{
									if($ret_type == 'OBJECT' && !isset($_obj->{$o}))
									{
											$_obj->{$o} = '';
									}
									elseif(!isset($_arr[$o]))
									{
											$_arr[$o] = '';
									}
							}
					}
					elseif($opt_list!='')
					{
							if($ret_type == 'OBJECT' && !isset($_obj->{$opt_list}))
							{
									$_obj->{$opt_list} = '';
							}
							elseif(!isset($_arr[$opt_list]))
							{
									$_arr[$opt_list] = '';
							}
					}

					return ($ret_type == 'OBJECT' ? $_obj : $_arr);
			}
	}
	
	if ( !function_exists('option_exists'))
	{
			function option_exists($name) {
					$_ci = & get_instance();
					$_ci->load->database(); 
					$dmc = $_ci->config->item('datamapper');
					$sql = "SELECT `id`
							FROM " . @$dmc['prefix'] . "options
							WHERE `name` = '".(trim($_ci->security->xss_clean($name)))."';";
					$query = $_ci->db->query($sql);
					return ($query->num_rows() > 0 ? true : false);
			}
	}
	
	if ( !function_exists('add_option'))
	{
			function add_option($name, $value) {
					$_ci = & get_instance();
					$_ci->load->database(); 
					$dmc = $_ci->config->item('datamapper');
					$_ci->db->insert(@$dmc['prefix'] . 'options', array('name'=>trim($_ci->security->xss_clean($name)), 'value'=>trim($_ci->security->xss_clean($value)))); 
			}
	}
	
	if ( !function_exists('update_options'))
	{
			function update_options($name, $value = '') {
					$_ci = & get_instance();
					$_ci->load->database(); 
					
					if(is_array($name) && count($name)>0)
					{
							foreach($name as $key=>$val)
							{
									if(!option_exists($key)) 
									{
											add_option($key, $val);
									}
									else
									{
											$dmc = $_ci->config->item('datamapper');
											$_ci->db->where('name', $key);
											$_ci->db->update(@$dmc['prefix'] . 'options', array('value'=>$val)); 
									}
							}
					}
					elseif($name!='')
					{
							if(!option_exists($name)) 
							{
									add_option($name, $value);
							}
							else
							{
									$dmc = $_ci->config->item('datamapper');
									$_ci->db->where('name', trim($_ci->security->xss_clean($name)));
									$_ci->db->update(@$dmc['prefix'] . 'options', array('value'=>$value));
							}
					}
			}
	}

/* End of file options_helper.php */
/* Location: ./application/helpers/options_helper.php */